<template>
    <div class="main-container">
      <el-form ref="formRef" label-suffix=":" label-width="200px">
        <div class="NSTitle">许可信息</div>
        <div class="p-30 py-10">
          <el-form-item label="设备序列号" prop="serialNum">
            <span>{{ licInfo.serialNum || "--" }}</span>
          </el-form-item>
          <el-form-item label="授权单位" prop="organ">
            <span>{{ licInfo.organ || "--" }}</span>
          </el-form-item>
          <el-form-item label="授权类型" prop="licenceTypeName">
            <span>{{ typeEmun[licInfo.licenceType] || "--" }}</span>
          </el-form-item>
          <el-form-item label="授权时间" prop="startDate">
            <span>
              {{ licInfo.startDate || "--" }}到{{
                licInfo.endDate || "--"
              }}</span
            >
          </el-form-item>
          <el-form-item label="维保时间" prop="matEndDate">
            <span>
              {{ licInfo.matStartDate || "--" }}到{{
                licInfo.matEndDate || "--"
              }}</span
            >
          </el-form-item>
        </div>
      </el-form>
  
      <div class="flex justify-evenly w-70% ml-15%">
        <el-button type="primary" @click="handleGet">下载授权申请文件</el-button>
        <el-upload
class="upload-demo"   :action="actionUrl" :limit="1" :show-file-list="false"
            :before-upload="beforeUpload" v-if="!loading">
            <el-button type="primary" size="large"   :loading="loading">上传授权许可文件</el-button>
          </el-upload>
          <el-button type="primary" size="large"   :loading="loading" v-else>上传中。。。</el-button>
      </div>
    </div>
  </template>
  <script setup lang="ts">
  import { getLicense ,uploadLicense , License}from '@/api/license'
  import { ElMessage, UploadRawFile } from "element-plus";
  import { Awaitable } from "element-plus/es/utils/typescript";
  const typeEmun = { 1: "正式", 2: "试用" };
  const loading = ref(false);
  const actionUrl: string = import.meta.env.VITE_UPLOAD_URL
  //下载授权申请文件
  const handleGet = (): void => {
    const link = document.createElement("a");
    link.href = "/admin-api/gateway/export/reqx";
    link.download = ""
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
};
  //上传
  const beforeUpload = (rawFile: UploadRawFile): Awaitable<void | undefined | null | boolean | File | Blob> => {
    loading.value = true;
    const formData = new FormData();
    formData.append("file", rawFile);
    uploadLicense(formData).then(res => {
      if(res.code === 200){
        loading.value = false;
        ElMessage.success("许可上传成功")
      }
    })
    return false;
};
  let licInfo = ref<License>({
      startDate: '',
      endDate: '',
      matStartDate: '',
      matEndDate: '',
      organ: '',
      licenceType: 0,
      licenceTypeName: '',
      status: '',
      statusName: '',
      msg: '',
      serialNum: ''
  });
  onMounted(async () => {
    let { data } = await getLicense();
    licInfo.value = data;
  });
  </script>
  
  <style scoped lang="scss"></style>
  